package com.zerosky.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zerosky.crm.po.Customer;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.Product;
import com.zerosky.crm.service.CustomerService;
import com.zerosky.crm.service.ProductService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

/**
 * 产品相关业务的Controller
 * 
 * @author 钟璟
 */
@Controller
public class ProductController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(ProductController.class); // 日志

	@Resource(name = "productService")
	private ProductService productService;

	@Resource(name = "customerService")
	private CustomerService customerService;

	/**
	 * 去产品管理
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toProductJsp.do")
	public String toProductJsp(HttpSession session, HttpServletRequest request) {
		return "WEB-INF/jsp/system/editProduct";
	}

	/**
	 * 去产品列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toListProduct.do")
	public String toListProduct(HttpSession session, HttpServletRequest request) {
		return "WEB-INF/jsp/product/listProduct";
	}

	/**
	 * 去产品预约审核列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/toProductCheck.do")
	public ModelAndView toContractCheckList(HttpSession session, HttpServletRequest request) throws Exception {
		//Employee user = (Employee) session.getAttribute("loginUser");
		//Map<String, Object> map = new HashMap<String, Object>();
		//map.put("status", 0);// 查询属于自己的信息
		//map.put("personLiable", user.getEmployeeId());// 查询属于自己的信息
		//List<Customer> list = customerService.getCustomerList(map);
		//List<Product> productList = productService.getProductList(map);// 获取所有产品信息
		//request.setAttribute("productList", productList);
		//request.setAttribute("customerList", list);
		return new ModelAndView("WEB-INF/jsp/product/contractCheckList");
	}

	@RequestMapping("/queryProductList.do")
	@ResponseBody
	public Map<String, Object> queryProductList(HttpSession session, HttpServletRequest request, Integer limit,
			Integer page, String productName, Integer status) throws Exception {
		PageInfo<Product> pageInfo = new PageInfo<Product>();
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> conditionMap = new HashMap<String, Object>();
		try {
			conditionMap.put("status", status);
			conditionMap.put("productName", productName);

			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			pageInfo = productService.getPageInfoList(conditionMap, pageInfo);

			json.put("data", pageInfo.getList());
			json.put("total", pageInfo.getPageCount());
			json.put("flag", 1);
		} catch (Exception e) {
			json.put("flag", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping(value = "/saveProduct.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveOrUpdateProduct(Product product, HttpServletRequest request,
			HttpServletResponse response, HttpSession session, MultipartFile fileGraphicsText) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 获取登录用户
			Employee user = (Employee) session.getAttribute("loginUser");
			String section = super.superiorCompany(user);
			// if (product.getHotelImgFile() != null) {//产品多附件上传
			// String path = "upload/";
			// MyFileUploadUtils.saveFile(request, product.getHotelImgFile(),
			// path);
			// if (request.getAttribute("picture") != null) {
			// product.setGraphicsText(path + request.getAttribute("picture"));
			// }
			// }
			product.setCreateDate(new Date());
			product.setSection(section);
			product.setCreatePeople(user.getName());

			Integer flag = productService.saveOrUpdate(product, "新增");

			if (flag > 0) {
				map.put("flag", 1);
				map.put("msg", "添加成功!");
			} else {
				map.put("flag", 0);
				map.put("msg", "添加失败,未知原因!");
			}
		} catch (Exception e) {
			map.put("flag", -1);
			map.put("msg", "程序出现异常,请联系管理员!");
			logger.error("获取数据失败，原因：" + e);
		}
		return map;
	}

	@RequestMapping(value = "deleteProduct.do")
	@ResponseBody
	public Map<String, Object> deleteProduct(HttpSession session, HttpServletRequest request,
			HttpServletResponse response, Integer productId) throws Exception {
		Map<String, Object> map = new HashMap<>();
		try {
			Integer flag = productService.deleteProduct(productId);
			if (flag > 0) {
				map.put("msg", "删除成功!");
				map.put("code", "success");
			} else {
				map.put("msg", "删除失败,请联系管理员!");
				map.put("code", "error");
			}
		} catch (Exception e) {
			map.put("msg", "删除失败，请稍后再试");
			map.put("code", "error");
			logger.error("获取数据失败，原因：" + e);
		}
		return map;
	}

	/**
	 * 
	 * 修改前根据id查询出来修改的产品
	 * 
	 * @param request
	 * @param productId
	 * @return
	 */
	@RequestMapping("toModifyProduct.do")
	@ResponseBody
	public Map<String, Object> toModifyProduct(HttpSession session, HttpServletRequest request, Integer productId) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Product product = productService.getProductById(productId);
			if (product != null) {
				// 数据
				json.put("flag", 1);
				json.put("data", product);
			} else {
				json.put("flag", 0);
				json.put("msg", "数据出现异常,请稍后再试！");
			}

		} catch (Exception e) {
			json.put("flag", "-1");
			json.put("msg", "系统异常,请联系管理员！");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping("modifyProduct.do")
	@ResponseBody
	public Map<String, Object> modifyProduct(HttpSession session, HttpServletRequest request, Product product) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// if (mfileGraphicsText.getSize() > 0) {
			// String path = "saituImage/";
			// boolean flag = MyFileUploadUtils.saveFile(request,
			// mfileGraphicsText, path);
			// if (flag == false) {
			// json.put("msg", "修改失败,图片上传失败!");
			// json.put("flag", 0);
			// return json;
			// }
			//
			// path += request.getAttribute("picture");
			// product.setGraphicsText(path); // 设置产品图片路径
			//
			// }

			Integer count = productService.saveOrUpdate(product, "修改");
			if (count > 0) {
				json.put("msg", "修改成功!");
				json.put("flag", 1);
			} else {
				json.put("msg", "修改失败!");
				json.put("flag", 0);
			}
		} catch (Exception e) {
			json.put("flag", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 查询全部产品信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/productList.do")
	@ResponseBody
	public Map<String, Object> productList(HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		// 获取所有产品信息
		List<Product> productList = productService.getProductList(json);
		json.put("data", productList);
		return json;
	}

	/*
	 * @RequestMapping("/redirectProduct.do") public ModelAndView
	 * redirectProduct(HttpSession session, HttpServletRequest request,
	 * HttpServletResponse response) throws Exception { ModelAndView mv = new
	 * ModelAndView(); String method = request.getParameter("method"); if
	 * ("add".equals(method)) {
	 * mv.setViewName("WEB-INF/jsp/product/addProduct"); } return mv; }
	 */

}
